<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Node.js," />










<meta name="description" content="Express中的路由模块化与静态资源托管使用nodemon来启动一个js文件 好处：每次改变保存文件，自动把已启动的文件更新（停止再重启），省略了用node app.js反复的Ctrl+C停止再重启的步骤 全局安装： 1npm install nodemon -g  为什么要使使路由模块化？  方便管理，降低耦合 太多的路由挂载，导致主文件体积庞大，不利于维护">
<meta property="og:type" content="article">
<meta property="og:title" content="express的模块化路由">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="Express中的路由模块化与静态资源托管使用nodemon来启动一个js文件 好处：每次改变保存文件，自动把已启动的文件更新（停止再重启），省略了用node app.js反复的Ctrl+C停止再重启的步骤 全局安装： 1npm install nodemon -g  为什么要使使路由模块化？  方便管理，降低耦合 太多的路由挂载，导致主文件体积庞大，不利于维护">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/get_01.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/post_01.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/hello_js.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/hello_txt.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/index_selected.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/public_mine.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/api_get.png">
<meta property="article:published_time" content="2022-04-25T12:19:33.000Z">
<meta property="article:modified_time" content="2022-04-25T13:38:25.318Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="Node.js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/get_01.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/04/25/express的模块化路由/"/>





  <title>express的模块化路由 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">express的模块化路由</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-04-25T20:19:33+08:00">
                2022-04-25
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/04/25/express的模块化路由/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Express中的路由模块化与静态资源托管"><a href="#Express中的路由模块化与静态资源托管" class="headerlink" title="Express中的路由模块化与静态资源托管"></a>Express中的路由模块化与静态资源托管</h1><p>使用<code>nodemon</code>来启动一个js文件</p>
<p>好处：每次改变保存文件，自动把已启动的文件更新（停止再重启），省略了用node app.js反复的Ctrl+C停止再重启的步骤</p>
<p>全局安装：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install nodemon -g</span><br></pre></td></tr></table></figure>

<p>为什么要使使路由模块化？</p>
<ol>
<li>方便管理，降低耦合</li>
<li>太多的路由挂载，导致主文件体积庞大，不利于维护</li>
</ol>
<span id="more"></span>

<h2 id="1-创建路由模块"><a href="#1-创建路由模块" class="headerlink" title="1.创建路由模块"></a>1.创建路由模块</h2><p>三步走战略</p>
<ul>
<li>导入express模块并创建路由对象</li>
<li>挂载路由实例</li>
<li>向外暴露路由对象</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 路由模块</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="comment">// 创建路由对象</span></span><br><span class="line"><span class="keyword">const</span> router =express.Router();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 挂载路由</span></span><br><span class="line">router.get(<span class="string">&#x27;/user/list&#x27;</span>,<span class="function">(<span class="params">request,response</span>)=&gt;</span>&#123;</span><br><span class="line">    response.send(<span class="string">&#x27;Get user list.&#x27;</span>)</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.post(<span class="string">&#x27;/user/add&#x27;</span>,<span class="function">(<span class="params">request,response</span>)=&gt;</span>&#123;</span><br><span class="line">    response.send(<span class="string">&#x27;Add new user&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 向外导出路由</span></span><br><span class="line"><span class="built_in">module</span>.exports = router</span><br></pre></td></tr></table></figure>

<h2 id="2-在服务器模块使用路由"><a href="#2-在服务器模块使用路由" class="headerlink" title="2.在服务器模块使用路由"></a>2.在服务器模块使用路由</h2><p>注册路由和使用路由</p>
<ul>
<li><p>导入路由模块</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">&#x27;./router&#x27;</span>);</span><br></pre></td></tr></table></figure></li>
<li><p>使用app.use()注册路由模块</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">app.use(router);</span><br></pre></td></tr></table></figure>

<p>完整代码</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="comment">// 导入写好的路由模块</span></span><br><span class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">&#x27;./router&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> app = express();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 把路由模块挂载到app的实例上</span></span><br><span class="line">app.use(router);</span><br><span class="line"><span class="comment">// 监听8008端口</span></span><br><span class="line">app.listen(<span class="number">8008</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;app is running http://127.0.0.1:8008&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>在postman测试结果</p>
<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/get_01.png" class title="get测试结果">

<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/post_01.png" class title="post测试结果">

<h2 id="3-如何托管静态资源"><a href="#3-如何托管静态资源" class="headerlink" title="3.如何托管静态资源"></a>3.如何托管静态资源</h2><p>假如我有一些文件，需要通过服务器接口去查看他，那么我该怎么做呢？</p>
<p>这就用到了express的静态资源托管</p>
<p>比如我想托管本目录下的files文件夹下的hello.txt文件，以及hello.js文件，那么我们可以这么做</p>
<p>使用express的static方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">express.static(<span class="string">&#x27;目标文件目录&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>代码如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> app = express();</span><br><span class="line"><span class="comment">// 静态资源托管</span></span><br><span class="line">app.use(express.static(<span class="string">&#x27;./files&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听8008端口</span></span><br><span class="line">app.listen(<span class="number">8008</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;app is running http://127.0.0.1:8008&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>这样，在浏览器输入 <a href="http://127.0.0.1:8008:hello.js的时候，就可以浏览到hello.js的内容，输入http://127.0.0.1:8008:hello.txt的时候也可以浏览到hello.txt的内容，输入http://127.0.0.1:8008:index_selected.png的时候，可以看到图片，如下：">http://127.0.0.1:8008:hello.js的时候，就可以浏览到hello.js的内容，输入http://127.0.0.1:8008:hello.txt的时候也可以浏览到hello.txt的内容，输入http://127.0.0.1:8008:index_selected.png的时候，可以看到图片，如下：</a></p>
<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/hello_js.png" class title="hello_js测试结果">

<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/hello_txt.png" class title="hello_txt测试结果">

<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/index_selected.png" class title="index_selected测试结果">

<p><strong>注意，此时是不加目录的，默认在该目录下的，也就是files,不需要再url中体现</strong></p>
<p>如果需要添加前缀目录，那可以这样，</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">app.use(<span class="string">&#x27;/public&#x27;</span>,express.static(<span class="string">&#x27;./files&#x27;</span>))</span><br></pre></td></tr></table></figure>

<p>此时，需要在地址栏中拼接入’**/public**‘，如下</p>
<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/public_mine.png" class title="public_mine测试结果">

<h2 id="4-为路由添加统一的访问前缀"><a href="#4-为路由添加统一的访问前缀" class="headerlink" title="4.为路由添加统一的访问前缀"></a>4.为路由添加统一的访问前缀</h2><p>只需要在app.use()中添加前缀，即可实现</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">app.use(<span class="string">&#x27;/api&#x27;</span>,router);         <span class="comment">// 以后的访问地址得加上http://localhost:8008/api/</span></span><br></pre></td></tr></table></figure>

<p>如下</p>
<img src="/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/api_get.png" class title="加上api访问前缀测试">

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="comment">// 导入写好的路由模块</span></span><br><span class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">&#x27;./router&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> app = express();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态资源托管</span></span><br><span class="line">app.use(<span class="string">&#x27;/public&#x27;</span>,express.static(<span class="string">&#x27;./files&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 把路由模块挂载到app的实例上</span></span><br><span class="line">app.use(<span class="string">&#x27;/api&#x27;</span>,router);         <span class="comment">// 以后的访问地址得加上http://localhost:8008/api/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听8008端口</span></span><br><span class="line">app.listen(<span class="number">8008</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;app is running http://127.0.0.1:8008&quot;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h2 id="5-app-use"><a href="#5-app-use" class="headerlink" title="5.app.use()"></a>5.app.use()</h2><p><strong>app.use的作用，就是注册全局中间件</strong>，这里的router和static都属于中间件，具体将在下期博客中做详细讲解</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/Node-js/" rel="tag"># Node.js</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/04/23/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%97%B6%E9%97%B4%E9%80%89%E6%8B%A9%E5%99%A8/" rel="next" title="WXminiProgram时间选择器">
                <i class="fa fa-chevron-left"></i> WXminiProgram时间选择器
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/04/25/express%E4%B9%8B%E4%B8%AD%E9%97%B4%E4%BB%B6/" rel="prev" title="express之中间件">
                express之中间件 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Express%E4%B8%AD%E7%9A%84%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%9D%97%E5%8C%96%E4%B8%8E%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E6%89%98%E7%AE%A1"><span class="nav-number">1.</span> <span class="nav-text">Express中的路由模块化与静态资源托管</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%88%9B%E5%BB%BA%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%9D%97"><span class="nav-number">1.1.</span> <span class="nav-text">1.创建路由模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%9C%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%A8%A1%E5%9D%97%E4%BD%BF%E7%94%A8%E8%B7%AF%E7%94%B1"><span class="nav-number">1.2.</span> <span class="nav-text">2.在服务器模块使用路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%A6%82%E4%BD%95%E6%89%98%E7%AE%A1%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90"><span class="nav-number">1.3.</span> <span class="nav-text">3.如何托管静态资源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%B8%BA%E8%B7%AF%E7%94%B1%E6%B7%BB%E5%8A%A0%E7%BB%9F%E4%B8%80%E7%9A%84%E8%AE%BF%E9%97%AE%E5%89%8D%E7%BC%80"><span class="nav-number">1.4.</span> <span class="nav-text">4.为路由添加统一的访问前缀</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-app-use"><span class="nav-number">1.5.</span> <span class="nav-text">5.app.use()</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/04/25/express%E7%9A%84%E6%A8%A1%E5%9D%97%E5%8C%96%E8%B7%AF%E7%94%B1/';
          this.page.identifier = '2022/04/25/express的模块化路由/';
          this.page.title = 'express的模块化路由';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
